# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Mateusz Mikuła <mati865@gmail.com>
# Contributor: wirx6 <wirx654@gmail.com>
# Contributor: Yuui Tanabe <yuuitanabe@163.com>
# Contributor: Oscar Fuentes <ofv@wanadoo.es>
# Contributor: Adrian Pop <adrian.pop@liu.se>


# We can switch to clang when it fully works with mingw-w64
_compiler=gcc
_generator="MSYS Makefiles"
#_generator="Ninja"
if [ "${_generator}" = "Ninja" ]; then
  _make_cmd="ninja"
else
  _make_cmd="make"
fi
_realname=clang
_url=https://releases.llvm.org
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-clang-analyzer"
         "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
         "${MINGW_PACKAGE_PREFIX}-compiler-rt"
         "${MINGW_PACKAGE_PREFIX}-libc++"
         "${MINGW_PACKAGE_PREFIX}-libc++abi"
         "${MINGW_PACKAGE_PREFIX}-libunwind"
         "${MINGW_PACKAGE_PREFIX}-lld"
         "${MINGW_PACKAGE_PREFIX}-lldb"
         "${MINGW_PACKAGE_PREFIX}-llvm"
         "${MINGW_PACKAGE_PREFIX}-openmp"
         "${MINGW_PACKAGE_PREFIX}-polly")
pkgver=9.0.0
pkgrel=5
pkgdesc="C language family frontend for LLVM (mingw-w64)"
arch=('any')
url="https://llvm.org/"
license=("custom:University of Illinois/NCSA Open Source License")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake>=3.4.3"
             "${MINGW_PACKAGE_PREFIX}-z3"
             "${MINGW_PACKAGE_PREFIX}-libffi"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-swig"
             "tar"
             "groff"
             $([[ "$_compiler" == "clang" ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-clang")
             $([[ "$_generator" == "Ninja" ]] && echo \
               "${MINGW_PACKAGE_PREFIX}-ninja")
             )
depends=("${MINGW_PACKAGE_PREFIX}-gcc"
         "${MINGW_PACKAGE_PREFIX}-uasm")
options=('!debug' 'strip')
source=(${_url}/$pkgver/llvm-${pkgver}.src.tar.xz{,.sig}
        ${_url}/$pkgver/openmp-${pkgver}.src.tar.xz{,.sig}
        ${_url}/$pkgver/cfe-${pkgver}.src.tar.xz{,.sig}
        ${_url}/$pkgver/compiler-rt-${pkgver}.src.tar.xz{,.sig}
        ${_url}/$pkgver/test-suite-${pkgver}.src.tar.xz{,.sig}
        ${_url}/$pkgver/libcxx-${pkgver}.src.tar.xz{,.sig}
        ${_url}/$pkgver/libcxxabi-${pkgver}.src.tar.xz{,.sig}
        ${_url}/$pkgver/clang-tools-extra-${pkgver}.src.tar.xz{,.sig}
        ${_url}/$pkgver/lld-${pkgver}.src.tar.xz{,.sig}
        ${_url}/$pkgver/lldb-${pkgver}.src.tar.xz{,.sig}
        ${_url}/$pkgver/libunwind-${pkgver}.src.tar.xz{,.sig}
        ${_url}/$pkgver/polly-${pkgver}.src.tar.xz{,.sig}
        "0001-mingw-w64-use-MSVC-style-ByteAlignment.patch"
        "0002-Fix-GetHostTriple-for-mingw-w64-in-msys.patch"
        "0004-llvm-config-look-for-unversioned-shared-lib-on-win32.patch"
        "0005-add-pthread-as-system-lib-for-mingw.patch"
        "0006-add-coff-exported-flag.patch"
        "0007-getLoadAddressWithOffset-assertion.patch"
        "0009-empty-target-prefix-only-msvc.patch"
        "0010-mbig-obj-for-all.patch"
        "0011-different-name-for-static-libraries.patch"
        "0012-fix-testplugin-linking.patch"
        "0013-set-cxx-version.patch"
        "0014-winpthreads-workaround.patch"
        "0101-Allow-build-static-clang-library-for-mingw.patch"
        "0102-fix-libclang-name-for-mingw.patch"
        "0103-Set-the-x86-arch-name-to-i686-for-mingw-w64.patch"
        "0104-link-pthread-with-mingw.patch"
        "0105-build-libclang-cpp-fix.patch"
        "0201-mingw-w64-__udivdi3-mangle-hack.patch"
        "0401-disable-visibility-annotations.patch"
        "0405-fix-conflict-win32-posix-threads.patch"
        "0406-fix-static_asserts-and-unique_ptr-for-libcxx.patch"
        "0501-fixes-to-get-lldb-building.patch"
        "0503-lldb-find-mingw-python.patch"
        "0504-fix-lldb-library-name-mingw.patch"
        "0505-use-gnu-printf.patch"
        "0506-mingw-python-posix-like.patch"
        "0507-declare-exports-for-all-windows-platforms.patch"
        "0508-add-share-header.patch"
        "0509-include-crtdbg.patch"
        "0510-fix-CMake-when-LLDB_DISABLE_PYTHON-if-ON-and-ep.patch"
        "0601-fix-static-assert-macro.patch"
        "0701-disable-visibility-annotations.patch"
        "0801-Don-t-build-LLVMPolly-on-WIN32.patch"
        "0901-openmp-kpm-lock.patch"
        "0902-libomp-weak-mingw.patch")
# Some patch notes :)
#0001-0099 -> llvm
#0101-0199 -> clang
#0201-0299 -> rt
#0301-0399 -> lld
#0401-0499 -> libc++
#0501-0599 -> lldb
#0601-0699 -> libunwind
#0701-0799 -> libc++abi
#0801-0899 -> polly
#0901-0999 -> openmp
sha256sums=('d6a0565cf21f22e9b4353b2eb92622e8365000a9e90a16b09b56f8157eabfe84'
            'SKIP'
            '9979eb1133066376cc0be29d1682bc0b0e7fb541075b391061679111ae4d3b5b'
            'SKIP'
            '7ba81eef7c22ca5da688fdf9d88c20934d2d6b40bfe150ffd338900890aa4610'
            'SKIP'
            '56e4cd96dd1d8c346b07b4d6b255f976570c6f2389697347a6c3dcb9e820d10e'
            'SKIP'
            '2ba3cde840c1c0ef744c93ae6a921207dbb9ccbb39f93f7ace9252e5203307cf'
            'SKIP'
            '3c4162972b5d3204ba47ac384aa456855a17b5e97422723d4758251acf1ed28c'
            'SKIP'
            '675041783565c906ac2f7f8b2bc5c40f14d871ecfa8ade34855aa18de95530e9'
            'SKIP'
            'ea1c86ce352992d7b6f6649bc622f6a2707b9f8b7153e9f9181a35c76aa3ac10'
            'SKIP'
            '31c6748b235d09723fb73fea0c816ed5a3fab0f96b66f8fbc546a0fcc8688f91'
            'SKIP'
            '1e4c2f6a1f153f4b8afa2470d2e99dab493034c1ba8b7ffbbd7600de016d0794'
            'SKIP'
            '976a8d09e1424fb843210eecec00a506b956e6c31adda3b0d199e945be0d0db2'
            'SKIP'
            'a4fa92283de725399323d07f18995911158c1c5838703f37862db815f513d433'
            'SKIP'
            '05f07589d2e255c1a867b5558bbbabad55a9d2ba8084b22f4dc5e08608f07aae'
            '9b6d3ecb0ef4a38d34aefaefff8c6257ff22d366d84630020d7f079dc8065d97'
            '1f318c0370357fdf9c54ae6d31bad761b0caa58ac099998937b636309ecb6590'
            '7f0c64cd87b61e894be632f180ae5291e1aa9f1d9d382608f659067eeeda7146'
            '2c2431997e664c5b42b359f4134a4773578753e5e25c505ba30de42d357f3057'
            '1887ea21fcd591a50dd10559774e872ce1183177e672b028133d9b669e3cac32'
            '33400d16d5f6671a8fd60345c3ae44b9777a7d600061957889d14305eb2ad709'
            '8c216c885a1cd84a63c842ac4ce7c857b1bb4952a46bbd9905adaadacb1a758a'
            '18e719f49af8704c66d7d5e0c0c94ab04a7206fe2a3d711eb652e0afb35ff52c'
            'c486e1d45f6fb2a24823d776d2b47d6930530d423df73cc635f863265210c294'
            '656007a5eb587ac51e8d953fd28522c53cc56d297f2fa87ba818a0fe2ebd2b76'
            'd32f3be9c0ac5a1fc7c187e2497af4359036c4d76a5123d73a9d06f75edc73b1'
            'b2e5fd29bf666998d495a321690c97fe239bb998abcf0739418ac0ea725fecd6'
            '01b029f2a21bd998ce374a90d41d214c891dfbb611dfbd9ca147517cd2c228ea'
            'ab49b90d69a609f441fa58e835873f2b1a8a060c9ca6358fee7d99c1973da692'
            '53646dd01af2862473e9719c5223366486268891ccbff86413943a432a8342e9'
            'a60f7328d84628a56a9f626e4dc26ffd0c35292c79eeba62ac3d4f25aef2fe5c'
            '5d6b066d63ea927b87e32b8e70a38f763373622ae4f6a5062db7a5e087b41faf'
            '219eca2ad015ababb05d81ed97b4b40cf13ea189ee93cf57aa814cca7cf40001'
            '2f8f3c819837e4a16a364ed6e2a0a879cd10f924169ed7d89c27c1ed64edfe95'
            'a219798d0199e0b34c90fcdacada7e0f6d179fa4349a26bce8f714173046ecc7'
            'bdb2d82c1508dd3ed79ec10fd265aca10c07dc27e63e72b8c0eeda9a777fb073'
            '37dedb324ea1204d360c6c75446a732b310c938926d0fb806b1e1f16d5e0432e'
            '9322401fa79f37bd746972f059ef9c07b96b97cb66c73a6290ed15853c9009d6'
            '7bbcca0ecd52e11401809fb543b8ba39a859ebeed52d15b672d50fff89de1a20'
            '9591d2890a4f4d1ac32d2032b7f03ca9151b7a2c2a29f281051a70c749e4a0f3'
            '78d52a2e11b3e6d0acbaab8b603921674dfed70062bf3aaf37ce1961de68d416'
            'dc656168d3e3067e57b342fbd9a9eb57dd95d8d8e540e7e66e48c3cea790bbb3'
            'f065285abd6bf5f67599a82f8c2338c57c1c5b4b3088b5d62c12356c3703f74e'
            '963387da0ff24e167c91d193def2f5a7581dca41166f73b8914fcf8fd636101f'
            'b9727a84f6e412ef9ace5f1b4030ccbc8789a6f1ef74cc09f95dfa9118515b8a'
            'eaa7ad87e3f0fc9ff697c8865cce5c501fcb3a71a9a29c9497e444013163976f'
            'bc394e597f8939b6f6630bd88c990f951738aaadacded2f3be71c658e9608fe7'
            'db8dc554f7780609ee4eb4bc9e3817d4a9409b622f6471c8db06c92705805ead'
            'f7958638e73273ea6a8df4a4e8726a0904d049fdaea7e84c33db5dec1af08fa8')
validpgpkeys=('B6C8F98282B944E3B0D5C2530FC3042E345AD05D'  # Hans Wennborg, Google.
              '474E22316ABF4785A88C6E8EA2C794A986419D8A') # Tom Stellard
noextract=(cfe-${pkgver}.src.tar.xz
           libcxx-${pkgver}.src.tar.xz
           lldb-${pkgver}.src.tar.xz
           test-suite-${pkgver}.src.tar.xz)

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  plain "Extracting clang-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/cfe-${pkgver} ]] && rm -rf ${srcdir}/cfe-${pkgver}
  [[ -d ${srcdir}/cfe-${pkgver} ]] || tar -xJvf ${srcdir}/cfe-${pkgver}.src.tar.xz -C ${srcdir} || true

  plain "Extracting libcxx-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/libcxx-${pkgver} ]] && rm -rf ${srcdir}/libcxx-${pkgver}
  [[ -d ${srcdir}/libcxx-${pkgver} ]] || tar -xJvf ${srcdir}/libcxx-${pkgver}.src.tar.xz -C ${srcdir} || true

  plain "Extracting lldb-${pkgver}.src.tar.xz due to symlink(s) without pre-existing target(s)"
  [[ -d ${srcdir}/lldb-${pkgver} ]] && rm -rf ${srcdir}/lldb-${pkgver}
  [[ -d ${srcdir}/lldb-${pkgver} ]] || tar -xJvf ${srcdir}/lldb-${pkgver}.src.tar.xz -C ${srcdir} || true

  cd "${srcdir}/llvm-${pkgver}.src"
  apply_patch_with_msg \
      "0001-mingw-w64-use-MSVC-style-ByteAlignment.patch" \
      "0002-Fix-GetHostTriple-for-mingw-w64-in-msys.patch" \
      "0004-llvm-config-look-for-unversioned-shared-lib-on-win32.patch" \
      "0005-add-pthread-as-system-lib-for-mingw.patch" \
      "0009-empty-target-prefix-only-msvc.patch" \
      "0010-mbig-obj-for-all.patch" \
      "0012-fix-testplugin-linking.patch" \
      "0013-set-cxx-version.patch" \
      "0014-winpthreads-workaround.patch" \

  # https://bugs.llvm.org/show_bug.cgi?id=25493
  apply_patch_with_msg \
      "0006-add-coff-exported-flag.patch"
  # https://bugs.llvm.org/show_bug.cgi?id=30584
  apply_patch_with_msg \
      "0007-getLoadAddressWithOffset-assertion.patch"

  cd "${srcdir}/cfe-${pkgver}.src"
  apply_patch_with_msg \
      "0101-Allow-build-static-clang-library-for-mingw.patch" \
      "0102-fix-libclang-name-for-mingw.patch" \
      "0103-Set-the-x86-arch-name-to-i686-for-mingw-w64.patch" \
      "0104-link-pthread-with-mingw.patch" \
      "0105-build-libclang-cpp-fix.patch"

  cd "${srcdir}/compiler-rt-${pkgver}.src"
  apply_patch_with_msg \
      "0201-mingw-w64-__udivdi3-mangle-hack.patch"

  cd "${srcdir}/libcxx-${pkgver}.src"
  apply_patch_with_msg \
      "0401-disable-visibility-annotations.patch" \
      "0405-fix-conflict-win32-posix-threads.patch" \
      "0406-fix-static_asserts-and-unique_ptr-for-libcxx.patch"

  cd "${srcdir}/lldb-${pkgver}.src"
  apply_patch_with_msg \
      "0501-fixes-to-get-lldb-building.patch" \
      "0503-lldb-find-mingw-python.patch" \
      "0504-fix-lldb-library-name-mingw.patch" \
      "0505-use-gnu-printf.patch" \
      "0506-mingw-python-posix-like.patch" \
      "0507-declare-exports-for-all-windows-platforms.patch" \
      "0508-add-share-header.patch" \
      "0509-include-crtdbg.patch" \
      "0510-fix-CMake-when-LLDB_DISABLE_PYTHON-if-ON-and-ep.patch"

  cd "${srcdir}/libunwind-${pkgver}.src"
  apply_patch_with_msg \
      "0601-fix-static-assert-macro.patch"

  cd "${srcdir}/libcxxabi-${pkgver}.src"
  apply_patch_with_msg \
      "0701-disable-visibility-annotations.patch"

  cd "${srcdir}/polly-${pkgver}.src"
  # With LLVM_LINK_LLVM_DYLIB=ON it's build before LLVM and fails with undefined references to LLVM functions
  apply_patch_with_msg \
      "0801-Don-t-build-LLVMPolly-on-WIN32.patch"

  cd "${srcdir}/openmp-${pkgver}.src"
  apply_patch_with_msg \
      "0901-openmp-kpm-lock.patch" \
      "0902-libomp-weak-mingw.patch"
    


  # At the present, clang must reside inside the LLVM source code tree to build
  # See https://bugs.llvm.org/show_bug.cgi?id=4840

  cd "${srcdir}"/llvm-${pkgver}.src
  rm -rf tools/clang tools/clang/tools/extra tools/lld tools/lldb tools/polly projects/compiler-rt projects/libcxx \
         projects/libcxxabi projects/libunwind projects/openmp projects/test-suite | true
  mv "${srcdir}/cfe-${pkgver}.src"               tools/clang
  mv "${srcdir}/clang-tools-extra-${pkgver}.src" tools/clang/tools/extra
  mv "${srcdir}/lld-${pkgver}.src"               tools/lld
  mv "${srcdir}/lldb-${pkgver}.src"              tools/lldb
  mv "${srcdir}/polly-${pkgver}.src"             tools/polly
  mv "${srcdir}/compiler-rt-${pkgver}.src"       projects/compiler-rt
  mv "${srcdir}/libcxxabi-${pkgver}.src"         projects/libcxxabi
  mv "${srcdir}/libcxx-${pkgver}.src"            projects/libcxx
  mv "${srcdir}/libunwind-${pkgver}.src"         projects/libunwind
  mv "${srcdir}/openmp-${pkgver}.src"            projects/openmp
  #mv "${srcdir}/testsuite-${pkgver}.src"         projects/test-suite
}

build() {
  cd "${srcdir}"

  [[ -d build-${CARCH} ]] && rm -rf build-${CARCH}
  mkdir build-${CARCH} && cd build-${CARCH}

  local -a extra_config

  if check_option "debug" "y"; then
    extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
    VERBOSE="VERBOSE=1"
  else
    extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  # Include location of libffi headers in CPPFLAGS
  FFI_INCLUDE_DIR="$(pkg-config --cflags libffi)"
  FFI_INCLUDE_DIR=$(echo $FFI_INCLUDE_DIR | sed 's|-I||g')

  # "Ninja" cant install each component seperately
  # https://github.com/martine/ninja/issues/932

  if [ "${_compiler}" == "clang" ]; then
    #export CC='clang -stdlib=libc++'
    #export CXX='clang++ -stdlib=libc++'
    export CC='clang'
    export CXX='clang++'
  fi
  
  export ASM_COMPILER="${MINGW_PREFIX}/bin/uasm.exe"
  if [ "${CARCH}" == "x86_64" ]; then
    export ASM_COMPILER_FLAGS=-win64
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"$_generator" \
    -DCMAKE_VERBOSE_MAKEFILE=ON \
    -DCMAKE_ASM_MASM_COMPILER="${ASM_COMPILER}" \
    -DLIBOMP_ASMFLAGS="${ASM_COMPILER_FLAGS}" \
    -DLIBOMP_FORTRAN_MODULES=ON \
    -DCMAKE_SYSTEM_IGNORE_PATH=/usr/lib \
    -DFFI_INCLUDE_DIR="${FFI_INCLUDE_DIR}" \
    -DCMAKE_C_FLAGS="${CFLAGS}" \
    -DCMAKE_CXX_FLAGS="${CXXFLAGS} -D_GNU_SOURCE" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCLANG_ANALYZER_BUILD_Z3=ON \
    -DLIBCLANG_BUILD_STATIC=ON \
    -DLIBCXX_ENABLE_SHARED=OFF \
    -DLIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF \
    -DLIBCXX_HAS_WIN32_THREAD_API=ON \
    -DLIBCXXABI_ENABLE_SHARED=OFF \
    -DLIBUNWIND_ENABLE_SHARED=OFF \
    -DLLVM_BUILD_STATIC=OFF \
    -DLLVM_BUILD_LLVM_DYLIB=ON \
    -DLLVM_LINK_LLVM_DYLIB=ON \
    -DLLVM_ENABLE_ASSERTIONS=OFF \
    -DLLVM_ENABLE_FFI=ON \
    -DLLVM_ENABLE_THREADS=ON \
    -DLLVM_ENABLE_SPHINX=ON \
    -DLLVM_POLLY_LINK_INTO_TOOLS=OFF \
    -DLLDB_RELOCATABLE_PYTHON=ON \
    -DLLDB_USE_SYSTEM_SIX=ON \
    -DLLDB_DISABLE_PYTHON=ON \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    "${extra_config[@]}" \
    ../llvm-${pkgver}.src

  # sed away the bad windows style command line parameters (they are translated by msys from /bad to c:\msys\bad
  sed -i.orig 's/\/c  \/Fo/-c -Fo/' projects/openmp/runtime/src/CMakeFiles/omp.dir/build.make
  sed -i.orig 's/\/safeseh \/coff/-safeseh -coff/' projects/openmp/runtime/src/CMakeFiles/omp.dir/build.make

  ${_make_cmd} VERBOSE=1

  # Disable automatic installation of components that go into subpackages
  # -i.orig to check what has been removed in-case it starts dropping more than it should
  #
  sed -i.orig '/\(clang\|lld\|lldb\|polly\)\/cmake_install.cmake/d' tools/cmake_install.cmake
  sed -i.orig '/\(extra\|scan-build\|scan-view\)\/cmake_install.cmake/d' tools/clang/tools/cmake_install.cmake
# sed -i.orig '/\(compiler-rt\|libcxxabi\|libcxx\)\/cmake_install.cmake/d' projects/cmake_install.cmake
  sed -i.orig '/\(compiler-rt\|libcxxabi\|libcxx\|openmp\|libunwind\)\/cmake_install.cmake/d' projects/cmake_install.cmake
  # fix the bad command line parameters, use unix style
}

#check() {
#  cd "${srcdir}"/build-${CARCH}
#  # Remove || true once testcase doesn't cause failures.
#  make check || true
#  make check-clang || true
#  make check-polly || true
#}

package_clang() {
  pkgdesc="C language family frontend for LLVM (mingw-w64)"
  url="https://clang.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-gcc"
           "${MINGW_PACKAGE_PREFIX}-z3")

  cd "${srcdir}/llvm-${pkgver}.src"
  ${_make_cmd} -C ../build-${CARCH}/tools/clang DESTDIR="${pkgdir}" install

  # Install static libraries
  install -Dm644 ../build-${CARCH}/lib/libclang.a ${pkgdir}${MINGW_PREFIX}/lib/libclang.a
  install -Dm644 ../build-${CARCH}/lib/libclang.a ${pkgdir}${MINGW_PREFIX}/lib/libclang_static.a
}

package_clang-analyzer() {
  pkgdesc="A source code analysis framework (mingw-w64)"
  url="https://clang-analyzer.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-clang=${pkgver}-${pkgrel}"
           "${MINGW_PACKAGE_PREFIX}-python")

  cd "${srcdir}/llvm-${pkgver}.src"
  ${_make_cmd} -C ../build-${CARCH}/tools/clang/tools/scan-build -j1 DESTDIR="${pkgdir}" install
  ${_make_cmd} -C ../build-${CARCH}/tools/clang/tools/scan-view -j1 DESTDIR="${pkgdir}" install

  # Use Python 2
  sed -i \
    -e 's|env python$|&2|' \
    -e 's|/usr/bin/python$|&2|' \
    "${pkgdir}"${MINGW_PREFIX}/bin/scan-view

  # Compile Python scripts
  ${MINGW_PREFIX}/bin/python -m compileall "${pkgdir}"${MINGW_PREFIX}/bin/clang-analyzer
  ${MINGW_PREFIX}/bin/python -O -m compileall "${pkgdir}"${MINGW_PREFIX}/lib/clang-analyzer
  install -Dm644 LICENSE.TXT "${pkgdir}"${MINGW_PREFIX}/share/licenses/clang-analyzer/LICENSE
}

package_clang-tools-extra() {
  pkgdesc="Extra tools built using Clang's tooling APIs (mingw-w64)"
  url="https://clang.llvm.org/"

  cd "${srcdir}/llvm-${pkgver}.src"
  ${_make_cmd} -C ../build-${CARCH}/tools/clang/tools/extra -j1 DESTDIR="${pkgdir}" install
}

package_compiler-rt() {
  pkgdesc="Runtime libraries for Clang and LLVM (mingw-w64)"
  url="https://compiler-rt.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}-${pkgrel}")

  cd "${srcdir}"/llvm-${pkgver}.src
  ${_make_cmd} -C ../build-${CARCH}/projects/compiler-rt DESTDIR="${pkgdir}" install
}

package_libcxx() {
  pkgdesc="C++ Standard Library (mingw-w64)"
  url="https://libcxx.llvm.org/"

  cd "${srcdir}/llvm-${pkgver}.src"
  ${_make_cmd} -C ../build-${CARCH}/projects/libcxx -j1 DESTDIR="${pkgdir}" install
}

package_openmp() {
  pkgdesc="OpenMP library (mingw-w64)"
  url="https://openmp.llvm.org/"

  cd "${srcdir}/llvm-${pkgver}.src"
  ${_make_cmd} -C ../build-${CARCH}/projects/openmp DESTDIR="${pkgdir}" install
}

package_libcxxabi() {
  pkgdesc="C++ Standard Library Support (mingw-w64)"
  url="https://libcxxabi.llvm.org/"
  # Don't depend on libunwind until it actually works as installing it breaks some packages
  #depends="${MINGW_PACKAGE_PREFIX}-libunwind"

  cd "${srcdir}/llvm-${pkgver}.src"
  ${_make_cmd} -C ../build-${CARCH}/projects/libcxxabi -j1 DESTDIR="${pkgdir}" install
}

package_libunwind() {
  pkgdesc='A new implementation of a stack unwinder for C++ exceptions (mingw-w64)'
  url='https://llvm.org/'

  cd "${srcdir}/llvm-${pkgver}.src"
  ${_make_cmd} -C ../build-${CARCH}/projects/libunwind -j1 DESTDIR="${pkgdir}" install
}

package_lld() {
  pkgdesc="Linker tools for LLVM (mingw-w64)"
  url="https://lld.llvm.org/"

  cd "${srcdir}/llvm-${pkgver}.src"
  ${_make_cmd} -C ../build-${CARCH}/tools/lld -j1 DESTDIR="${pkgdir}" install
}

package_lldb() {
  pkgdesc="Next generation, high-performance debugger (mingw-w64)"
  url="https://lldb.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
           "${MINGW_PACKAGE_PREFIX}-llvm"
           "${MINGW_PACKAGE_PREFIX}-python"
           "${MINGW_PACKAGE_PREFIX}-readline"
           "${MINGW_PACKAGE_PREFIX}-swig")

  cd "${srcdir}/llvm-${pkgver}.src"

  ${_make_cmd} -C ../build-${CARCH}/tools/lldb DESTDIR="${pkgdir}" install

  # Compile Python scripts
   local _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  ${MINGW_PREFIX}/bin/python -m compileall "${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages/lldb"
  ${MINGW_PREFIX}/bin/python -O -m compileall "${pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages/lldb"

  install -Dm644 tools/lldb/LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/lldb/LICENSE"
}

package_llvm() {
  pkgdesc="Low Level Virtual Machine (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-libffi"
           "${MINGW_PACKAGE_PREFIX}-z3"
           "${MINGW_PACKAGE_PREFIX}-gcc-libs") # "compiler-rt"

  cd "${srcdir}"/llvm-${pkgver}.src

  ${_make_cmd} -C ../build-${CARCH} DESTDIR="${pkgdir}" install

  # TODO: why CMake haven't included it?
  cp ../build-${CARCH}/lib/libLLVM.dll.a "${pkgdir}/${MINGW_PREFIX}/lib/"

  install -Dm644 LICENSE.TXT "${pkgdir}${MINGW_PREFIX}/share/licenses/llvm/LICENSE"

  # Install CMake stuff
  install -d "${pkgdir}"${MINGW_PREFIX}/share/llvm/cmake/{modules,platforms}
  install -Dm644 "${srcdir}"/llvm-${pkgver}.src/cmake/modules/*.cmake "${pkgdir}"${MINGW_PREFIX}/share/llvm/cmake/modules/
  install -Dm644 "${srcdir}"/llvm-${pkgver}.src/cmake/platforms/*.cmake "${pkgdir}"${MINGW_PREFIX}/share/llvm/cmake/platforms/

  # FileCheck is needed to build rust.
  install -Dm755 "${srcdir}"/build-${CARCH}/bin/FileCheck.exe "${pkgdir}${MINGW_PREFIX}/bin/FileCheck.exe"
  # fox cmake files.
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${pkgdir}/${MINGW_PREFIX}/lib/cmake/llvm/LLVMExports.cmake
}

package_polly() {
  pkgdesc="Polly - Polyhedral optimizations for LLVM (mingw-w64)"
  url="https://polly.llvm.org/"
  depends=("${MINGW_PACKAGE_PREFIX}-llvm=${pkgver}-${pkgrel}")

  cd "${srcdir}"/llvm-${pkgver}.src
  ${_make_cmd} -C ../build-${CARCH}/tools/polly DESTDIR="${pkgdir}" install
  # fox cmake files.
  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  local SRC_WIN=$(cygpath -wm ${srcdir})
  sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}/|g" \
    -e "s|${SRC_WIN}/build-${CARCH}/lib|${MINGW_PREFIX}/lib|g" \
    -i "${pkgdir}/${MINGW_PREFIX}/lib/cmake/polly/PollyConfig.cmake"
}

# Wrappers
package_mingw-w64-i686-clang(){
  package_clang
}

package_mingw-w64-i686-clang-analyzer(){
  package_clang-analyzer
}

package_mingw-w64-i686-clang-tools-extra(){
  package_clang-tools-extra
}

package_mingw-w64-i686-compiler-rt(){
  package_compiler-rt
}

package_mingw-w64-i686-libc++abi(){
  package_libcxxabi
}

package_mingw-w64-i686-libc++(){
  package_libcxx
}

package_mingw-w64-i686-openmp(){
  package_openmp
}

package_mingw-w64-i686-lld(){
  package_lld
}

package_mingw-w64-i686-lldb(){
  package_lldb
}

package_mingw-w64-i686-libunwind(){
  package_libunwind
}

package_mingw-w64-i686-llvm(){
  package_llvm
}

package_mingw-w64-i686-polly(){
  package_polly
}

package_mingw-w64-x86_64-clang(){
  package_clang
}

package_mingw-w64-x86_64-clang-analyzer(){
  package_clang-analyzer
}

package_mingw-w64-x86_64-clang-tools-extra(){
  package_clang-tools-extra
}

package_mingw-w64-x86_64-compiler-rt(){
  package_compiler-rt
}

package_mingw-w64-x86_64-libc++abi(){
  package_libcxxabi
}

package_mingw-w64-x86_64-libc++(){
  package_libcxx
}

package_mingw-w64-x86_64-openmp(){
  package_openmp
}


package_mingw-w64-x86_64-lld(){
  package_lld
}

package_mingw-w64-x86_64-lldb(){
  package_lldb
}

package_mingw-w64-x86_64-libunwind(){
  package_libunwind
}

package_mingw-w64-x86_64-llvm(){
  package_llvm
}

package_mingw-w64-x86_64-polly(){
  package_polly
}
